#include <stdlib.h>

#include "lib.h"

#include "slotguts.h"
#include "frozenguts.h"
#include "frozenslotscommon.h"
#include "slotspin.h"
#include "getrand.h"


bool8 
FrozenSlotGuts::isValidGuts()
{
    return ((getGutsId() >= FrozenCommon::GUTS_MIN) && (getGutsId() <= FrozenCommon::GUTS_MAX));
}


void
FrozenSlotGuts::loadSubGameData()
{
	SlotGuts::loadSubGameData();

	// If the amount < 500 make it 500
	if(m_subGame->getData(FrozenCommon::SGIDX_AMOUNT) < FrozenCommon::BASE_AMOUNT)
		 m_subGame->update(FrozenCommon::SGIDX_AMOUNT, FrozenCommon::BASE_AMOUNT);
}


bool8 FrozenSlotGuts::saveGame()
{
	int32 gamenum;
	bool8	success;

	success = SlotGuts::saveGame ();
 	gamenum = getSlotSpin((this->getSlotSpinCount()-1))->getGameNum();
	archiveSubGameData(gamenum);

	if (m_subGame->getData (FrozenCommon::SGIDX_LEVEL) == FrozenCommon::SNOWMAN_MAX_LEVEL)
	{
		m_subGame->update(FrozenCommon::SGIDX_LEVEL, 0);
		m_subGame->update(FrozenCommon::SGIDX_AMOUNT, FrozenCommon::BASE_AMOUNT);
	}
	return (success);
}

void
FrozenSlotGuts::spin(SlotSpin *slotSpin)
{
    SlotGuts::spin(slotSpin);
    SpinLine *spinLine = slotSpin->getSpinLineAt(0);

	// Have we completed the snowman?
	if (m_subGame->getData (FrozenCommon::SGIDX_LEVEL) == FrozenCommon::SNOWMAN_MAX_LEVEL)
	{
		spinLine->payoutamount = m_subGame->getData (FrozenCommon::SGIDX_AMOUNT) * this->getInfo()->creditValue;
//        m_subGame->update(FrozenCommon::SGIDX_LEVEL, 0);
//        m_subGame->update(FrozenCommon::SGIDX_AMOUNT, FrozenCommon::BASE_AMOUNT);
	}
}

uint8
FrozenSlotGuts::classifyStops(uint8* stops,uint8 bet, uint8 line)
{
	uint8 result = FrozenCommon::EVT_LOSER;

	int numSeven = 0;
	int numSnowman = 0;
	int numBlueHat = 0;
	int numRedHat = 0;
	int numSnowFlake = 0;
	int numSun = 0;
	int numPenguin = 0;

    //  Do a preliminary run-through
    for ( uint8 i = 0; i < getReelCount(); i++ )
    {
        // Set the icon flags;
        switch ( getIcon(i, stops[i])->iconid )
        {
			case FrozenCommon::SNOWFLAKE:
				++numSnowFlake;
				break;
			case FrozenCommon::SUN:
				++numSun;
				break;
			case FrozenCommon::PENGUIN:
				++numPenguin;
				break;
			case FrozenCommon::BLUEHAT:
				++numBlueHat;
				break;
			case FrozenCommon::REDHAT:
				++numRedHat;
				break;
			case FrozenCommon::SNOWMAN:
				++numSnowman;
				break;
			case FrozenCommon::SEVEN:
				++numSeven;
				break;
        }
    }

	if ((numSeven + numPenguin) == 3)
	{
		result = FrozenCommon::EVT_3SEVEN;
	}
	if (((numSnowman + numPenguin) == 3) && (result == FrozenCommon::EVT_LOSER))
	{
		result = FrozenCommon::EVT_3SNOWMAN;
	}
	if (((numRedHat + numPenguin) == 3) && (result == FrozenCommon::EVT_LOSER))
	{
		result = FrozenCommon::EVT_3REDHAT;
	}
	if (((numBlueHat + numPenguin) == 3) && (result == FrozenCommon::EVT_LOSER))
	{
		result = FrozenCommon::EVT_3BLUEHAT;
	}
	if (((numRedHat + numBlueHat + numPenguin) == 3) && (result == FrozenCommon::EVT_LOSER))
	{
		result = FrozenCommon::EVT_3HAT;
	}

	if ((result == FrozenCommon::EVT_LOSER) && (bet == 3))
	{
		//update the number of dials in the subgamedata
		int level;

		level = m_subGame->getData(FrozenCommon::SGIDX_LEVEL) - numSun;
		if (level < 0)
			level = 0;
		level += numSnowFlake;
		if (level > FrozenCommon::SNOWMAN_MAX_LEVEL)
			level = FrozenCommon::SNOWMAN_MAX_LEVEL;
        m_subGame->update(FrozenCommon::SGIDX_LEVEL, level);
        m_subGame->update(FrozenCommon::SGIDX_AMOUNT, m_subGame->getData(FrozenCommon::SGIDX_AMOUNT) + numSun);
	}

	return result;
}
